package com.igeekhome.ccs.biz.impl;

import com.igeekhome.ccs.biz.ICustomerServiceRoleBiz;
import com.igeekhome.ccs.entity.CustomerServiceGroup;
import com.igeekhome.ccs.entity.CustomerServiceRole;
import com.igeekhome.ccs.mapper.CustomerServiceRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CustomerServiceRoleBizImpl implements ICustomerServiceRoleBiz {
    @Autowired
   private CustomerServiceRoleMapper mapper;

//👇zyh-----------------------------------------------
    //查询所有角色
     @Override
        public List<CustomerServiceRole> query() {
         return mapper.query();
        }

    //模糊匹配
    @Override
    public List<CustomerServiceRole> matchCsrwithString(String str) {
        List<CustomerServiceRole> totalList = mapper.query();
        List<CustomerServiceRole> resultList = new ArrayList<CustomerServiceRole>();
        Iterator<CustomerServiceRole> iter = totalList.iterator();
        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
            //模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(totalList.get(i).getRoleName());
            if (matcher.find() ) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }

    //删除单个角色
    @Override
    public int deleteCsrById(int csrId) {
         mapper.deleteCsrById(csrId);
        return 0;
    }

    //批量删除角色
    @Override
    public int deleteCsrByList(List<Integer> list) {
        for(int i = 0;i<list.size();i++){
            mapper.deleteCsrById(list.get(i));
        }
        return 0;
    }

    //新增角色
    @Override
    public int createNewCsr(CustomerServiceRole customerServiceRole,int csId) throws ParseException {
         //id自增，名字、描述、主页显示、管理权限已有

        //名字去空格,描述无所谓
        String str= customerServiceRole.getRoleName();
        str=str.trim();
        customerServiceRole.setRoleName(str);

         //初始化人数
         customerServiceRole.setRoleNumber(0);
         //创建者
        customerServiceRole.setCreateId(csId);
         //创建时间
        Date now=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNow = df.format(now);
        Date dateNow = df.parse(strNow);
        customerServiceRole.setCreateTime((java.util.Date) dateNow);

        mapper.createNewCsr(customerServiceRole);
        return 0;
    }
    //通过角色id拿到角色信息
    @Override
    public CustomerServiceRole queryCsrById(int csrId) {
        return mapper.queryCsrById(csrId);
    }

    //编辑角色信息
    @Override
    public int updateCsr(String newRoleName, String newRoleDesc, int newP1, int newP2, int csrId) throws ParseException {
        //更新时间
        Date now=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strUpdate = df.format(now);
        Date dateUpdate = df.parse(strUpdate);
        //名字去空格
        newRoleName = newRoleName.trim();
        mapper.updateCsr(newRoleName,newRoleDesc,newP1,newP2,csrId,dateUpdate);
        return 0;
    }

    @Override
    public int queryCsrByName(String name) {
        return mapper.queryCsrByName(name);
    }
//👆zyh-----------------------------------------------
}
